import request from '@/utils/request'
import type { ApiResponse } from '@/types/customer'

// 定义分页响应类型
interface PaginatedResponse<T> {
  list: T[]
  total: number
  page: number
  limit: number
}

// 工单类型定义
interface WorkOrder {
  id: number
  work_order_no: string
  order_no: string
  customer_name: string
  category: number
  title: string
  status: number
  priority: number
  assignee_name?: string
  department_manager_name?: string
  create_time: number
  start_time?: number
  end_time?: number
  actual_end_time?: number
  description: string
}

// 工单进度类型
interface WorkOrderProgress {
  id: number
  operator_name: string
  progress_type: number
  content: string
  create_time: number
  old_status?: number
  new_status?: number
}

// 查询参数类型
interface WorkOrderQueryParams {
  page?: number
  limit?: number
  category?: number
  status?: number
  assigneeId?: number
}

// 用户选项类型
interface UserOption {
  id: number
  name: string
}

export const workorderApi = {
  // 获取工单列表
  getWorkOrderList: (
    params: WorkOrderQueryParams
  ): Promise<ApiResponse<PaginatedResponse<WorkOrder>>> => {
    return request({
      url: '/api/workorder/workOrder/list',
      method: 'get',
      params,
    })
  },

  // 获取工单详情
  getWorkOrderDetail: (id: number): Promise<ApiResponse<WorkOrder>> => {
    return request({
      url: `/api/workorder/workOrder/detail/${id}`,
      method: 'get',
    })
  },

  // 获取工单进度列表
  getWorkOrderProgress: (id: number): Promise<ApiResponse<{ list: WorkOrderProgress[] }>> => {
    return request({
      url: `/api/workorder/workOrder/progress/${id}`,
      method: 'get',
    })
  },

  // 分配工单
  assignWorkOrder: (id: number, data: { assignee_id: number }): Promise<ApiResponse<any>> => {
    return request({
      url: `/api/workorder/workOrder/assign/${id}`,
      method: 'post',
      data,
    })
  },

  // 添加进度
  addProgress: (id: number, data: { content: string }): Promise<ApiResponse<any>> => {
    return request({
      url: `/api/workorder/workOrder/progress/${id}`,
      method: 'post',
      data,
    })
  },

  // 完成工单
  completeWorkOrder: (id: number): Promise<ApiResponse<any>> => {
    return request({
      url: `/api/workorder/workOrder/complete/${id}`,
      method: 'post',
    })
  },

  // 获取用户列表（用于分配）
  getUserOptions: (): Promise<ApiResponse<UserOption[]>> => {
    return request({
      url: '/api/user/user/options',
      method: 'get',
    })
  },
}
